package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.*;
import com.dongdongshop.admin.vo.AddressVO;
import com.dongdongshop.admin.vo.AreaVO;
import com.dongdongshop.admin.vo.CitiesVO;
import com.dongdongshop.admin.vo.ProvincesVO;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.util.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper provincesMapper;
    @Autowired
    private TbCitiesMapper citiesMapper;
    @Autowired
    private TbAreasMapper areasMapper;



    @Override
    public List<AddressVO> getByUserId(String userName) {
        TbAddressExample e = new TbAddressExample();
        TbAddressExample.Criteria criteria = e.createCriteria();
        criteria.andUserIdEqualTo(userName);
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(e);
        return tbAddresses.stream().map(s->{
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(s, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void add(AddressVO addressVO, LoginUser tbUser) {
        TbAddress address = new TbAddress();
        BeanUtils.copyProperties(addressVO, address);
        address.setUserId(tbUser.getUname());
        tbAddressMapper.insertSelective(address);
    }

    public TbProvinces getByPid(String cityId){
        List<TbProvinces> tbProvinces = provincesMapper.selectByExample(null);
        return tbProvinces.stream().filter(vo -> Objects.equals(cityId, vo.getProvinceid())).findFirst().orElse(null);


    }
    public TbCities getByCid(String cityId){
        List<TbCities> tbCities = citiesMapper.selectByExample(null);
        return tbCities.stream().filter(vo -> Objects.equals(cityId, vo.getCityid())).findFirst().orElse(null);

    }
    public TbAreas getByAid(String cityId){
        List<TbAreas> tbAreas = areasMapper.selectByExample(null);
        return tbAreas.stream().filter(vo -> Objects.equals(cityId, vo.getAreaid())).findFirst().orElse(null);
    }

    @Override
    public List<AddressVO> getAll() {
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(null);
        return tbAddresses.stream().map(m->{
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(m, vo);
            vo.setProvinceName(getByPid(vo.getProvinceId()).getProvince());
            vo.setCityName(getByCid(vo.getCityId()).getCity());
            vo.setTownName(getByAid(vo.getTownId()).getArea());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public AddressVO getAddrById(Long id) {
        TbAddress tbAddress = tbAddressMapper.selectByPrimaryKey(id);
        AddressVO vo = new AddressVO();
        BeanUtils.copyProperties(tbAddress, vo);
        return vo;
    }
}
